<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product_model extends MY_Model {

	var $table = 'product';
	
	//构造函数
	public function __construct() {
		//调用父类构造函数，必不可少
		parent::__construct();
	}

	/**
	 * 取得所有商品
	 * 
	 * @param array $config
	 * @param array $like
	 * @return array
	 */
	public function getProductAll($config = array(), $like = array()) {
		$config['totalCount'] = $this->get_search_num(NULL, $like);
		if($config['offset'] >= $config['totalCount']) {
			$config['offset'] = 0;
			$config['pageNum'] = 1;
 		}
 		$data = $config;
		$config['select'] = 'product.*, a.img as thumbnail, a1.img as hot_img,ca.name as catename';
		$config['join'][] = array('album a','product.thumbnail_id=a.id','left');
		$config['join'][] = array('album a1','product.hot_img_id=a1.id','left');
		$config['join'][] = array('category ca','ca.id=product.category_id','left');
		$data['data'] = $this->search(NULL, $config, $like);
		return $data;
	}

	public function addMulti($data) {
		$this->db->trans_start();
		$dataUpdate = array();
		foreach ($data as $v) {
			$dataInsert = $v;
			$result = $this->db->insert('product', $dataInsert);
			if($result) {
				$dataUpdate[] = array(
					'id' => $v['thumbnail_id'],
					'source_id'=>$this->db->insert_id(),
				);
			}
		}
		$this->db->update_batch('album', $dataUpdate, 'id');
		$this->db->trans_complete();
		return $this->db->trans_status();
	}
	/**
	 * 增加商品
	 * 
	 * @param array $data 商品基本信息
	 * @param array $specs 商品规格
	 * @param array $images 描述中的所有图片
	 * @return boolean
	 */
	public function addProduct($data,$specs,$images) {
		$this->db->trans_start();
		//插入商品信息
		$thumbnail = $data['thumbnail'];
		$hotImg = $data['hot_img'];
		unset($data['thumbnail']);
		unset($data['hot_img']);
		$this->db->insert($this->table, $data);
		$id = $this->db->insert_id();

		//插入规格
		if(!empty($specs)) {
			foreach ($specs as &$v) {
				$v['product_id'] = $id;
				$v['created_at'] = time();
			}
			$this->db->insert_batch('specification', $specs);
		}
		
		$images[] = $thumbnail;
		$images[] = $hotImg;
		//从临时图片中取出商品图片
		if(!empty($images)) {
			$this->db->where_in('img',$images);
			$this->db->where(array('from'=>'1', 'source_id'=>0)); //来源商品、未分配ID
			$this->db->update('album', array('source_id'=>$id));
		}
		
		$this->db->trans_complete();
		return $this->db->trans_status()?$id:false;
	}
	
	/**
	 * 删除商品
	 * 
	 * @param string $ids 多个用逗号隔开
	 * @return integer
	 */
	public function deleteProduct($ids) {
		$idsArr = explode(',', $ids);
		$this->db->trans_start();
		$this->db->where_in('id', $idsArr);
		$this->db->delete($this->table);
		
		$this->db->where_in('product_id', $idsArr);
		$this->db->delete('specification');
		
		$this->db->where_in('source_id', $idsArr);
		$this->db->where(array('from'=>'1'));
		$query = $this->db->select('img')->from('album')->get();
		$rows = $query->result_array();
		if(!empty($rows)) {
			foreach ($rows as $v) {
				if(file_exists($v['img'])) {
					unlink($v['img']);
				}
			}
			$this->db->where_in('source_id', $idsArr);
			$this->db->where(array('from'=>'1'));
			$this->db->delete('album');
		}
		$this->db->trans_complete();
		return $this->db->trans_status();
	}

	/**
	 * 更新商品信息
	 * 
	 * @param array $data
	 * @param integer|array $id
	 * @return integer
	 */
	public function updateProduct($data, $id) {
		return $this->update($data, array('id'=>$id));
	}

	/**
	 * 查找详细信息
	 * 
	 * @param integer $id
	 * @return array
	 */
	public function findProduct($id) {
		$query = $this->db->select('p.*, a.img as thumbnail, a1.img as hot_img')
				->from($this->table . ' p')
				->join('album a', 'p.thumbnail_id=a.id', 'left')
				->join('album a1', 'p.hot_img_id=a1.id', 'left')
				->where(array('p.id'=>$id))->get();
		return $query->row_array();
	}
	
	/**
	 * 检查是否超过库存数量
	 *
	 * @param array $data
	 * @return boolean
	 */
	public function checkNumber($data)
	{
		foreach ($data as $da)
		{
			return $this->get_row(array('id'=>$da['product_id'],'stock <'=>$da['request_number']));
		}
	}
}
